package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File articleDir;

    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }
    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在发表文章....");
        //获取参数
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);

        if (title == null || content == null || author == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }
        File articleFile = new File(articleDir, title + ".obj");
        if (articleFile.exists()) { //标题不能重复
            File file = new File("./webapps/myweb/article_info_error.html");
            response.setEntity(file);
            return;
        }
        Article article = new Article(title, author, content);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(
                                articleFile
                        )
                )
        ) {
            oos.writeObject(article);
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文章发表完毕！");
    }

    /**
     * 生成文章的动态页面
     */
    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("生成文章列表");
        //创建集合存放article返回序列化对象
        List<Article> articleList = new ArrayList<>();
        //获取末尾以.obj结尾的文件放到articles数组中
        File[] subs = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFile : subs) { //遍历集合后缀为.obj的文件反序列为对象放入集合中
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(articleFile)
                    )
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articleList.forEach(u -> System.out.println(u));

        //生成页面
        //按行写字符串到指定文件，文件不存在会自动创建
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("    <meta charset=\"UTF-8\">");
        pw.println("    <title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("    <center>");
        pw.println("        <h1>文章列表</h1>");
        pw.println("        <table border=\"1\">");
        pw.println("            <tr>");
        pw.println("                <td>标题</td>");
        pw.println("                <td>作者</td>");
        pw.println("            </tr>");

        for (Article article : articleList) {
            pw.println("            <tr>");
            pw.println("                <td>" + article.getTitle() + "</td>");
            pw.println("                <td>" + article.getAuthor() + "</td>");
            pw.println("            </tr>");
        }

        pw.println("        </table>");
        pw.println("    </center>");
        pw.println("</body>");
        pw.println("</html>");
        pw.flush();

        response.setContentType("text/html");

        System.out.println("生成页面完毕！");


    }

}
